/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xrkol.sharelib.dao.AttendanceDao;
import com.xrkol.sharelib.dao.ClassDao;
import com.xrkol.sharelib.dao.TeachingDao;
import com.xrkol.sharelib.entity.Attendance;
import com.xrkol.sharelib.entity.Clazz;
import com.xrkol.sharelib.entity.Student;
import com.xrkol.sharelib.entity.Teacher;
import com.xrkol.sharelib.entity.Teaching;
import com.xrkol.sharelib.service.ClassService;
import com.google.common.collect.Lists;

/**
 * @author peng.xia
 * 
 */
@Service
@Transactional
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassDao classDao;

    @Autowired
    private AttendanceDao attendanceDao;

    @Autowired
    private TeachingDao teachingDao;

    @Override
    public void save(Clazz entity) {
        this.classDao.save(entity);
    }

    @Override
    public void update(Clazz entity) {
        this.classDao.update(entity);
    }

    @Override
    public void delete(String id) {
        this.classDao.delete(id);
    }

    @Override
    public Clazz findById(String id) {
        return this.classDao.findById(id);
    }

    @Override
    public List<Clazz> getClassesByAdviser(Teacher currentUser) {
        return this.classDao.getClassesByAdviser(currentUser);
    }

    @Override
    public List<Clazz> getAllClasses(String schoolId) {
        return this.classDao.getAllClasses(schoolId);
    }

    @Override
    public List<Clazz> findClassesWithPagination(String schoolId, Integer page) {
        return this.classDao.findClassesWithPagination(schoolId, page);
    }

    @Override
    public List<Clazz> findAllClassesByStudents(List<Student> students) {
        List<Clazz> clazzs = Lists.newArrayList();
        for (Student student : students) {
            Attendance attendance = this.attendanceDao.getByStudent(student.getId());
            Clazz clazz = attendance.getclazz();
            if (!clazzs.contains(clazz)) {
                clazzs.add(clazz);
            }
        }
        return clazzs;
    }

    @Override
    public Clazz findClassByAdviser(Teacher currentTeacher) {
        return this.classDao.findClassByAdviser(currentTeacher);
    }

    @Override
    public Clazz getClassByStudent(Student student) {
        return this.attendanceDao.getByStudent(student.getId()).getclazz();
    }

    @Override
    public List<Clazz> getClassesByTeacher(Teacher teacher) {
        List<Teaching> teachings = this.teachingDao.getByTeacher(teacher);
        List<Clazz> adviseClasses = this.classDao.getClassesByAdviser(teacher);
        List<Clazz> classes = Lists.newArrayList(adviseClasses);

        for (Teaching teaching : teachings) {
            Clazz clazz = teaching.getclazz();
            if (!classes.contains(clazz)) {
                classes.add(clazz);
            }
        }

        return classes;
    }

    @Override
    public List<Clazz> getTeachingClassesByTeacher(Teacher teacher) {
        List<Teaching> teachings = this.teachingDao.getByTeacher(teacher);
        List<Clazz> teachingClazzs = new ArrayList<Clazz>();
        for (Teaching t : teachings) {
            teachingClazzs.add(t.getclazz());
        }
        return teachingClazzs;
    }

    @Override
    public boolean checkExist(Clazz clazz, String schoolId) {
        return this.classDao.checkExist(clazz.getClassName(), schoolId);
    }

}
